
var DocsPollerPack = window.DocsPollerPack || {};

DocsPollerPack.ViewInterface = function(){};

DocsPollerPack.ViewInterface.prototype.notify = function(){
        alert("notify: Not Implemented function");
}
DocsPollerPack.ViewInterface.prototype.addControlSuscriptor = function(controlSubs){
        alert("addControlSuscriber: Not Implemented function");
}
DocsPollerPack.ViewInterface.prototype.getDocKey = function(controlSubs){
        alert("getDocKey: Not Implemented function");
}
DocsPollerPack.ViewInterface.prototype.writeTable = function(){
      alert("onClick: Not implemented function");
}

DocsPollerPack.View = function(mod, button, button2){
        this.suscriptor = null;
        this.model = mod;
        //this.panel = panel;
        this.boton = button;
		this.boton2 = button2;
		
		var self = this;
		this.boton.onclick = function() {
			

				self.onClick();

        };
		this.boton2.onclick = function() {
			
		
				self.onClick2();
			
			
        };
}

DocsPollerPack.View.prototype = clone(DocsPollerPack.ViewInterface.prototype);

DocsPollerPack.View.prototype.constructor = DocsPollerPack.View;

DocsPollerPack.View.prototype.addControlSuscriptor = function(controlSubs){
        this.suscriptor = controlSubs;
}
DocsPollerPack.View.prototype.notify = function(){
        var obj = this.model.getTable();
		this.writeTable(obj);
}
DocsPollerPack.View.prototype.onClick = function(boton){
        document.getElementById("tableArea").innerHTML = "<CENTER>CARGANDO</CENTER>"
		this.suscriptor.notify();
}

DocsPollerPack.View.prototype.onClick2 = function(boton){
        document.getElementById("tableArea").innerHTML = "<CENTER>CARGANDO</CENTER>"
		this.suscriptor.notifyRegion();
}
DocsPollerPack.View.prototype.writeTable = function(array){		
	var totalVotes = 0;			
	var auxCandidate = array[1]
	var auxVotes = array[2];
	var newIndexA;
	var newIndexB;
	for(var i=1; i<auxVotes.length; i++){             
		for(var j=0; j<((auxVotes.length)-1); j++){                     
           if(auxVotes[j]<auxVotes[j+1]){                                    
            newIndexA = auxVotes[j];
			newIndexB = auxCandidate[j];
			auxVotes[j] = auxVotes[j+1];
			auxCandidate[j] = auxCandidate[j+1];
            auxVotes[j+1] = newIndexA;
			auxCandidate[j+1] = newIndexB;
           }
        }
    }
	var header = "<TABLE ALIGN="+"CENTER>"+"<tr><th><PRE>Candidato   </PRE></th><th> Votos Obtenidos </th></tr>";		
	for(i in auxVotes){
		totalVotes = totalVotes+auxVotes[i];
	}		
	for (i in array[1]){
		header = header+"<tr><td>"+array[1][i]+""+"</td>"+"<td>"+auxVotes[i]+""+"</td></tr>";
	}
	var feet = "<tr><td>Total Votos</td><td>"+totalVotes+"</td>"+"<tr><td>Zona</td><td>"+array[0]+"</td></tr></TABLE>";
	//feet += 	
	document.getElementById("tableArea").innerHTML = header+feet;
	window.drawChart(auxCandidate,auxVotes);
}

